﻿using System;

namespace StockAnalyst2.Chart
{
	internal partial class OptionsDialog : IDisposable
	{
		#region Properties

		public new OptionsVM DataContext
		{
			get
			{
				return (OptionsVM)base.DataContext;
			}
			set
			{
				if (this.DataContext != value)
				{
					if (this.DataContext != null)
					{
						this.DataContext.DialogCloseRequested -= this.Options_DialogCloseRequested;
					}
					value.DialogCloseRequested += this.Options_DialogCloseRequested; // Disposed

					base.DataContext = value;
				}
			}
		}

		#endregion

		#region Event Handlers

		private void Options_DialogCloseRequested(object sender, Utils.Wpf.DialogCloseRequestedEventArgs e)
		{
			this.DialogResult = e.DialogResult;
			this.Close();
		}

		#endregion

		#region Public Methods

		public OptionsDialog()
		{
			InitializeComponent();
		}

		#endregion

		#region IDisposable

		protected bool Disposed { get; private set; }

		private void Dispose(bool disposing)
		{
			if (!this.Disposed)
			{
				this.InternalDispose(disposing);
			}

			this.Disposed = true;
		}

		protected virtual void InternalDispose(bool disposing)
		{
			if (disposing)
			{
				if (this.DataContext != null)
				{
					this.DataContext.DialogCloseRequested -= this.Options_DialogCloseRequested;
				}
			}

			// Dispose here only unmanaged objects 
			// Don’t use managed objects here because maybe 
			// they have been finalized already
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		~OptionsDialog()
		{
			this.Dispose(false);
		}

		#endregion

	}
}
